package com.ww.springboot.boot.algorithm.leetcode1;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-30 09:15
 */
public class BY908最小差值I {

    public static void main(String[] args) {

        int[] nums = {0,10};
        int k = 2;

        System.out.println(smallestRangeI(nums, k));
    }

    /**
     * 找到最大值和最小值
     *
     * @param nums
     * @param k
     * @return
     */
    public static int smallestRangeI(int[] nums, int k) {
        int min = nums[0];
        int max = nums[0];

        for (int i = 1; i < nums.length; i++) {

            int num = nums[i];

            if (num < min) {
                min = num;
            }

            if (num > max) {
                max = num;
            }
        }

        if(max - min > k*2){
            return max - min - k*2;
        }
        return 0;
    }
}
